package com.twu.biblioteca;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ysotirov
 * Date: 25/01/13
 * Time: 10:39
 * To change this template use File | Settings | File Templates.
 */

/** A class that holds the logic of the biblioteca application */
public class Biblioteca {
    List<book> booksList;
    List<Movie> moviesList;
    List<Member> membersList;
    String activeMemberID = "";
    Menu bMenu;
    IOInterface ioHandler;
    boolean exit = false;

    String[] titles = {"Star Wars", "The lord of the rings", "The Hobbit", "Inception",
    "Shutter Island", "Pretty Woman", "Wall-e", "Madagascar", "Django Unchained", "The Lige of Pi",
    "The Godfather", "Ice Age", "Cruel Intentions", "Sherlock Holmes", "Troy"};

    int[] years = {1974, 2000, 1983, 1999, 2011, 2008, 2008, 2001, 2010,1997, 1990, 1991, 1993, 1994, 1995};

    String[] dirs = {"Director One", "Director Two", "Director Three", "Director Four",
    "Director Five", "Director Six", "Director Seven", "Director Eight", "Director Nine",
    "Director Ten", "Director Eleven", "Director Twelve", "Director Thirteen",
    "Director Fourteen", "Director Fifteen"};
    public Member activeMember;

    public Biblioteca(){
        booksList = new ArrayList<book>();
        ioHandler = new IOHandler();
        bMenu = new MainMenu(ioHandler, this);
        moviesList = new ArrayList<Movie>();
        membersList = new ArrayList<Member>();

        populateBooksList();
        populateMoviesList();
        populateMembersList();
    }

    public void populateMoviesList() {
        for (int i=0; i<titles.length; i++)
        moviesList.add(new Movie(titles[i], years[i], dirs[i]));

    }

    /** The startup logic of the application */
    public void StartApp() {
        //bMenu.biblioteca = this;
        int choice=0;
        ioHandler.queueMsg("Welcome to the Biblioteca Application!");
        getUserId();
        while (!exit) {
        bMenu.displayMenu();
        choice = bMenu.getUserChoice();
        bMenu.executeOption(choice);
        ioHandler.queueMsg("");
     //   bMenu.titleMsg="Main Menu" ;
        }

    }

    public void getUserId() {
        ioHandler.queueMsg("Please input your library ID");
        ioHandler.Print();
        activeMemberID = ioHandler.getUserCredentials();
        while (!isAMember(activeMemberID))
        {
            ioHandler.queueMsg("Please input a valid library ID");
            ioHandler.Print();
            ioHandler.getUserCredentials();
        }
    }

    public void populateMembersList(){
        membersList.add(new Member("111-1111", "pass1"));
        membersList.get(0).setDetails("David Weber", "dweber@gmail.com", "0982648392");
        membersList.add(new Member("111-1112", "passssss"));
        membersList.get(1).setDetails("Amanda Lorans", "alorans@gmail.com", "08274937439");
        membersList.add(new Member("111-1113", "1pass2"));
        membersList.get(2).setDetails("Rachel Bilson", "bilson@gmail.com", "079287843920");

    }

    public boolean isAMember(String activeMemberID) {
        for (int i=0; i<membersList.size(); i++){
            if (membersList.get(i).libraryID.contains(activeMemberID)){
                activeMember = membersList.get(i);
                return true;
            }
        }
        return false;
    }


    /**Hardcoded list of books */
    public void populateBooksList()
    {
        booksList.add(new book("Head First Java "));
        booksList.add(new book("The Art of Agile Development "));
        booksList.add(new book("Agile Samurai "));
        booksList.add(new book("Don't Make me Think "));
        booksList.add(new book("Pragmatic Thinking & Learning "));
    }



}
